﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinQ_DTO;
using System.Xml.Linq;
//MinhKhoa
namespace DAO
{
    public class SanPhamGocDAO
    {
        public List<shop_sanphamgoc> LayTatCaSanPhamGoc()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var products = from a in EFashionDB.shop_sanphamgocs select a;
            return (List<shop_sanphamgoc>)products.ToList();
        }
        public void ThemSanPhamGoc(shop_sanphamgoc AddObject)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            EFashionDB.shop_sanphamgocs.InsertOnSubmit(AddObject);
            EFashionDB.SubmitChanges();
        }
        public void XoaSanPhamGoc(int Ma)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var ListResultQuery = from a in EFashionDB.shop_sanphamgocs
                                    where a.MaSanPhamGoc == Ma
                                        select a;
            shop_sanphamgoc Result = ListResultQuery.Single();
            EFashionDB.shop_sanphamgocs.DeleteOnSubmit(Result);
            EFashionDB.SubmitChanges();
        }
        public void CapNhatSanPhamGoc(int Ma,shop_sanphamgoc EditObject)
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var ListResultQuery = from a in EFashionDB.shop_sanphamgocs
                          where a.MaSanPhamGoc == Ma
                          select a;
            shop_sanphamgoc Result = ListResultQuery.Single();

            Result.DonGia = EditObject.DonGia;
            Result.HinhSanPhamGoc = EditObject.HinhSanPhamGoc;
            Result.LoaiSanPhamGoc = EditObject.LoaiSanPhamGoc;
            Result.MaSanPhamGoc = EditObject.MaSanPhamGoc;
            Result.Phai = EditObject.Phai;
            Result.TenSanPhamGoc = EditObject.TenSanPhamGoc;
            EFashionDB.SubmitChanges();
        }
        public List<shop_sanphamgoc> LayTatCaSanPhamGocNam()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var products = from a in EFashionDB.shop_sanphamgocs
                           where a.Phai == true
                           select a;
            return products.ToList();
        }
        public List<shop_sanphamgoc> LayTatCaSanPhamGocNu()
        {
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var products = from a in EFashionDB.shop_sanphamgocs
                           where a.Phai == false
                           select a;
            return products.ToList();
        }
        public int LayMaSanPhamGoc(String src)
        {
            XElement xmlTemp;
            xmlTemp = new XElement("root",
                new XElement("src", src));
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var products = from a in EFashionDB.shop_sanphamgocs
                           select a;
            List<shop_sanphamgoc> aTemp = products.ToList();
            for (int i = 0; i < aTemp.Count; i++)
            {
                if(aTemp[i].HinhSanPhamGoc.Value.CompareTo(xmlTemp.Value)==0)
                    return aTemp[i].MaSanPhamGoc;    
            }
            return -1;
        }
        public shop_sanphamgoc LaySanPhamGoc(int ma)
        {
            
            EFashionDBDataContext EFashionDB = new EFashionDBDataContext();
            var products = from a in EFashionDB.shop_sanphamgocs
                           where a.MaSanPhamGoc == ma
                           select a;
            shop_sanphamgoc Result = products.Single();
            return Result;
        }
    }
}
